﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ComicIsland.App.Controls
{
    public partial class XProgressBar : UserControl
    {
        public readonly static DependencyProperty IsOnProgressProperty = DependencyProperty.Register("IsOnProgress", 
            typeof(bool), 
            typeof(XProgressBar), 
            new PropertyMetadata(false, OnIsOnProgressChanged));

        public readonly static DependencyProperty IsShowTextProperty = DependencyProperty.Register("IsShowText", 
            typeof(Visibility), 
            typeof(XProgressBar), 
            new PropertyMetadata((object)((Visibility)0), OnIsShowTextChanged));

        public readonly static DependencyProperty TextProperty = DependencyProperty.Register("Text", 
            typeof(string), 
            typeof(XProgressBar), 
            new PropertyMetadata("", OnTextChanged));

        

        public bool IsOnProgress
        {
            get
            {
                return (bool)base.GetValue(XProgressBar.IsOnProgressProperty);
            }
            set
            {
                base.SetValue(XProgressBar.IsOnProgressProperty, value);
            }
        }

        public Visibility IsShowText
        {
            get
            {
                return (Visibility)base.GetValue(XProgressBar.IsShowTextProperty);
            }
            set
            {
                base.SetValue(XProgressBar.IsShowTextProperty, value);
            }
        }

        public string Text
        {
            get
            {
                return (string)base.GetValue(XProgressBar.TextProperty);
            }
            set
            {
                base.SetValue(XProgressBar.TextProperty, value);
            }
        }


        public XProgressBar()
        {
            InitializeComponent();
        }

        protected void OnIsOnProgressChanged(DependencyPropertyChangedEventArgs e)
        {
            this.MoreProgressBar.IsIndeterminate = (this.IsOnProgress);
            if (!this.IsOnProgress)
            {
                this.txtProgressBarText.SetValue(UIElement.VisibilityProperty, (Visibility)1);
                return;
            }
            this.txtProgressBarText.SetValue(UIElement.VisibilityProperty, this.IsShowText);
        }

        private static void OnIsOnProgressChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((XProgressBar)d).OnIsOnProgressChanged(e);
        }

        protected void OnIsShowTextChanged(DependencyPropertyChangedEventArgs e)
        {
            this.txtProgressBarText.SetValue(UIElement.VisibilityProperty, this.IsShowText);
        }

        private static void OnIsShowTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((XProgressBar)d).OnIsShowTextChanged(e);
        }

        protected void OnTextChanged(DependencyPropertyChangedEventArgs e)
        {
            this.txtProgressBarText.SetValue(XProgressBar.TextProperty, (string)e.NewValue);
        }

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((XProgressBar)d).OnTextChanged(e);
        }
    }
}